package com.sgb.kjwl.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPhotoView2Binding;
import com.sgb.knowledgeshare.weight.AlivcVideoPlayView;
import com.sgb.knowledgeshare.weight.ControllerView;
import com.swgk.core.base.model.entity.VideoBean;
import java.io.Serializable;
import java.util.List;

public class PhotoViewActivity2 extends AppActivity {
    ActivityPhotoView2Binding binding;

    private static final String KEY_URL = "url";
    private static final String KEY_TYPE = "type";
    private static final String KEY_CONTENT = "content";
    private static final String KEY_NAME = "key_name";
    private static final String KEY_DATA = "key_data";
    /**
     * 视频数据源
     */
    private static String VIDEOLISTBEAN = "video_list_bean";
    private List<VideoBean> adapterList;

    private int mType;
    private String mUrl;
    private String mContent;
    private String mName;
    private List<VideoBean> mVideoList;
    @Override
    protected boolean statusBarDark() {
        return false;
    }

    //图片
    public static void launch(Context context, String url, int type, String content, String name){
        Intent intent = new Intent(context, PhotoViewActivity2.class);
        intent.putExtra(KEY_URL, url);
        intent.putExtra(KEY_TYPE, type);
        intent.putExtra(KEY_CONTENT, content);
        intent.putExtra(KEY_NAME, name);
        context.startActivity(intent);
    }
    //视频
    public static void launch(Context context, List<VideoBean> videoBeanList, int type, String content, String name){
        Intent intent = new Intent(context, PhotoViewActivity2.class);
        intent.putExtra(KEY_DATA, (Serializable) videoBeanList);
        intent.putExtra(KEY_TYPE, type);
        intent.putExtra(KEY_CONTENT, content);
        intent.putExtra(KEY_NAME, name);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void init() {
        if (getIntent() != null){
            mType = getIntent().getIntExtra(KEY_TYPE,1);
            mContent = getIntent().getStringExtra(KEY_CONTENT);
            mUrl = getIntent().getStringExtra(KEY_URL);
            mName = getIntent().getStringExtra(KEY_NAME);
            mVideoList = (List<VideoBean>) getIntent().getSerializableExtra(KEY_DATA);
        }
        binding.tvContent.setText(mContent);
        if (mType == 1){
            binding.photoView.setVisibility(View.VISIBLE);
            binding.videoPlayDetailView.setVisibility(View.GONE);
            binding.tvType.setText(TextUtils.isEmpty(mName) ? "这是一张图片" : mName);
            binding.photoView.enable();
            RequestOptions options = new RequestOptions()
                    .fitCenter();
            Glide.with(this)
                    .load(mUrl)
                    .apply(options)
                    .into(binding.photoView);
        } else {
            binding.photoView.setVisibility(View.GONE);
            binding.videoPlayDetailView.setVisibility(View.VISIBLE);
            binding.tvType.setText(TextUtils.isEmpty(mName) ? "这是一段视频" : mName);
            binding.videoPlayDetailView.setOnRefreshDataListener(null);
            if (null == mVideoList) return;
            binding.videoPlayDetailView.refreshVideoList(mVideoList, 0);
            adapterList = binding.videoPlayDetailView.getAdapterList();
            binding.videoPlayDetailView.setShowUser(false);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        binding.videoPlayDetailView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.videoPlayDetailView.onPause();
    }

    @Override
    public void onDestroy() {
        binding.videoPlayDetailView.onDestroy();
        super.onDestroy();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(PhotoViewActivity2.this, R.layout.activity_photo_view_2);
        binding.actionBarImageLeft.setOnClickListener(v -> finish());
        binding.videoPlayDetailView.setCallback(new AlivcVideoPlayView.UserVideoCallback() {
            @Override
            public void UserVideoController(ControllerView controller, VideoBean videoBean) {

            }

            @Override
            public void VideoInfo(int pos, VideoBean video) {

            }

            @Override
            public void UserVideoLike(VideoBean bean, ControllerView controller) {

            }
        });
    }

}
